<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'排课时间段列表'),
	array('op'=>'edit','title'=>'添加排课时间段'),	
);
$return['storetype']=$this->storetype;
$return['years']=array();
for($i=0;$i<=5;$i++){
	$year=date('Y',time())+$i;
	$return['years'][$year]=$year;
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['area_id'])){
		$params['area_id']=intval($_RQ['area_id']);
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(!empty($_RQ['storetype'])){
		$params['storetype']=intval($_RQ['storetype']);
		$return['params']['storetype']=$_RQ['storetype'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_kexie_lesson_paitime',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			if($item['type']){
				$item['storetype']=pdo_getcolumn('jy_kexie_store',array('id'=>$item['storeid']),'type');				
			}else{
				$item['storeid']=0;
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'tip'=>'该类型的学校，只能在指定的时间段内排课！','list'=>array(
			array('name'=>'storeid','title'=>'所属学校','type'=>'select','options'=>'stores'),
			array('name'=>'storetype','title'=>'校区类型','type'=>'radio','options'=>'storetype'),
			array('name'=>'starttime','title'=>'选课开启时间','type'=>'time'),
			array('name'=>'endtime','title'=>'选课结束时间','type'=>'time'),
			array('name'=>'year','title'=>'年份','type'=>'radio','options'=>'years'),
		),'action'=>array(
		)),
		'search'=>array(
			array('name'=>'storetype','width'=>10,'title'=>'校区类型','type'=>'select','options'=>'storetype'),
			array('name'=>'title','width'=>10,'title'=>'课程名称','type'=>'string'),
		)
	);
	$return['stores']=pdo_getall('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_lesson_paitime',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['starttime']=date('Y-m-d',$item['starttime']);
		$item['endtime']=date('Y-m-d',$item['endtime']);
	}else{
		$item=array('type'=>0);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回课程列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'type','title'=>'单个学校排？','type'=>'switch'),
					array('name'=>'storeid','title'=>'选择学校','type'=>'select_more','options'=>'stores','show'=>array('type'=>'1')),
					array('name'=>'storetype','title'=>'校区类型','type'=>'radio','options'=>'storetype','show'=>array('type'=>'0')),
					array('name'=>'starttime','title'=>'选课开启时间','type'=>'time'),
					array('name'=>'endtime','title'=>'选课结束时间','type'=>'time'),
					array('name'=>'year','title'=>'年份','type'=>'radio','options'=>'years'),
				)
			),
		)
	);
	$return['stores']=pdo_getall('jy_kexie_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['starttime'] || !$postdata['endtime']){
		exi('填写：选课开启时间，选课结束时间！','error');
	}
	$data=array_fields(array('type','storeid','storetype','starttime','endtime','year'),$postdata);
	$id=intval($postdata['id']);
	$data['starttime']=strtotime($data['starttime']);
	$data['endtime']=strtotime($data['endtime']);
	if($id){
		pdo_update('jy_kexie_lesson_paitime',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_kexie_lesson_paitime',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kexie_lesson_paitime',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}